﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Drawing;

namespace H2.NET.Barcode
{

    class Pattern
    {
        public static Pen pen = new Pen(Color.Black);
        public static Brush brush = Brushes.Black;
        private bool[] nw = new bool[9];

        public static Pattern Parse(string s)
        {
            Debug.Assert(s != null);

            s = s.Replace(" ", "").ToLower();

            Debug.Assert(s.Length == 9);
            Debug.Assert(s.Replace("n", "").Replace("w", "").Length == 0);

            Pattern p = new Pattern();

            int i = 0;
            foreach (char c in s)
                p.nw[i++] = c == 'w';

            return p;
        }

        public int GetWidth(Code39Settings settings)
        {
            int width = 0;

            for (int i = 0; i < 9; i++)
                width += (nw[i] ? settings.WideWidth : settings.NarrowWidth);

            return width;
        }

        public int Paint(Code39Settings settings, Graphics g, int left)
        {

#if DEBUG
            Rectangle gray = new Rectangle(left, 0, GetWidth(settings), settings.BarCodeHeight + settings.TopMargin + settings.BottomMargin);
            g.FillRectangle(Brushes.White, gray);
#endif
            int x = left;

            int w = 0;
            for (int i = 0; i < 9; i++)
            {
                int width = (nw[i] ? settings.WideWidth : settings.NarrowWidth);

                if (i % 2 == 0)
                {
                    Rectangle r = new Rectangle(x, settings.TopMargin, width, settings.BarCodeHeight);
                    g.FillRectangle(brush, r);
                }

                x += width;
                w += width;
            }

            return w;
        }
    }
}
